#-*-Makefile-*- helps emacs recognize that this file is a Makefile

##########################################################################
#
# Special handling of the MAKECMDGOALS macro is performed during
# 'make install' ...  so it gets its own Makefile.install.
# This file works with Makefile.extended and doesn't do much on its own
#
##########################################################################

##########################################################################
# Rule to make the installation directories. (include, lib, bin, etc.)
# These directories must exist before the files that they will contain
# can be produced.  However, those things that depend upon the existence
# of thses directories should never be considered "out-of-date" with
# respect to them.  (in general, the modification time of a directory will
# be newer than that of its contents)
#
# The rule that forces make to look to this one is given in 
# Makefile.extended where Makefile depends on $REQD_DIRS
# 'mkdir -p' ensures that parent directories will be created
##########################################################################

%/.exists :
	@if [ ! -d $* ]; then echo Creating directory:$*; mkdir -p $*; fi
	-@chmod -f 770 $*
	@touch $*/.exists

##########################################################################
# Function that defines how header files are copied to their read-only
# include/ location.  (used in Makefile.extended rules)
##########################################################################
define copy_header
if [ -f $@ ]; then $(RM) $@; fi
echo "copying $< to $@"
cp -f $< $@
chmod a-w $@
endef

##########################################################################
# Function that defines how scripts are copied to their read-only
# csh_script/ location.  (used in Makefile.extended rules)
##########################################################################
define copy_script
if [ -f $@ ]; then $(RM) $@; fi
echo "copying $< to $@"
cp -f $< $@
chmod a-w $@
chmod a+x $@
endef

##########################################################################
# The rest of this file applies only when 'install' appears as a target
# when make is invoked
##########################################################################

install_yes := $(findstring install,$(MAKECMDGOALS))
ifeq ($(install_yes),install)

##########################################################################
# strip out 'install' and set the default list of things to install
INSTALL_TARGETS := ${strip ${filter-out install, $(MAKECMDGOALS)}}
install_LIBS:=$(LIBRARIES)
install_DSO :=$(DSO_NAME)
install_BINS:=$(EXECUTABLES)
install_INCS:=$(INCLUDES)

ifneq ($(INSTALL_TARGETS),)

##########################################################################
# A target was specified, as in 'make install blah'...  set the list
# of things to install accordingly
install_LIBS:=${strip ${filter %.a,$(INSTALL_TARGETS)}}
install_DSO:=${strip ${filter %.so,$(INSTALL_TARGETS)}}
install_INCS:=${strip ${filter %.h %.inc,$(INSTALL_TARGETS)}}
not_BINS:= $(install_LIBS) $(install_DSO) $(install_INCS) $(TARGETS)
install_BINS:=${strip ${filter-out $(not_BINS),$(INSTALL_TARGETS)}}

##########################################################################
# Check for 'make install libs'
install_libs_yes := ${findstring libs, $(INSTALL_TARGETS)}
ifeq ($(install_libs_yes),libs)
install_LIBS:=$(LIBRARIES)
install_DSO:=$(DSO_NAME)
endif

##########################################################################
# Check for 'make install include'
install_incs_yes := ${findstring include, $(INSTALL_TARGETS)}
ifeq ($(install_incs_yes),include)
install_INCS:=$(INCLUDES)
endif

##########################################################################
# Check for 'make install scripts'
install_scripts_yes := ${findstring scripts, $(INSTALL_TARGETS)}
ifeq ($(install_scripts_yes),scripts)
install_SHXS:=$(SCRIPTS)
endif

##########################################################################
# Check for 'make install all'
install_all_yes := ${findstring all, $(INSTALL_TARGETS)}
ifeq ($(install_all_yes),all)
install_LIBS:=$(LIBRARIES)
install_DSO :=$(DSO_NAME)
install_BINS:=$(EXECUTABLES)
install_INCS:=$(INCLUDES)
endif

##########################################################################
# this simple rule ensures that the rest of the targets in MAKECMDGOALS
# are effectively ignored.  clean and veryclean have their own rule that
# should not be over-ridden. 'all' 'include' and 'libs' 
##########################################################################
$(filter-out clean veryclean, $(INSTALL_TARGETS)):
	@echo nothing >> /dev/null

endif

INSTALL_LIBS:=${install_LIBS:%=$(INSTALL_LIBDIR)/%}
INSTALL_DSO :=${install_DSO:%=$(INSTALL_DSODIR)/%}
INSTALL_BINS:=${install_BINS:%=$(INSTALL_BINDIR)/%}
INSTALL_INCS:=${install_INCS:%=$(INSTALL_INCDIR)/%}
INSTALL_SHXS:=${install_SHXS:%=$(INSTALL_SHXDIR)/%}

##########################################################################
# Static pattern rules to copy libraries and executables from the user's 
# local "working copy" directories to the installed "group copy" dirs.
##########################################################################

$(INSTALL_BINS) : $(INSTALL_BINDIR)/% : $(BINDIR)/%
	@echo "copying $< to $@"
	@cp $< $@

$(INSTALL_LIBS) : $(INSTALL_LIBDIR)/%.a : $(LIBDIR)/%.a 
	@echo "copying $< to $@"
	@cp $< $@

$(INSTALL_DSO) : $(INSTALL_DSODIR)/%.so : $(DSODIR)/%.so
	@mkdir -p $(dir $@)
	@echo "copying $< to $@"
	@cp $< $@

##########################################################################
# Static pattern rule to copy header files to installed include/ directory
##########################################################################

$(INSTALL_INCS) : $(INSTALL_INCDIR)/% : ./%
	@$(copy_header)

##########################################################################
# Static pattern rule to copy scripts to installed csh_script/ directory
##########################################################################

$(INSTALL_SHXS) : $(INSTALL_SHXDIR)/% : ./%
	@$(copy_script)

$(INSTALL_INCS) : $(INSTALL_INCDIR)/.exists
$(INSTALL_SHXS) : $(INSTALL_SHXDIR)/.exists
$(INSTALL_LIBS) : $(INSTALL_LIBDIR)/.exists
$(INSTALL_BINS) : $(INSTALL_BINDIR)/.exists

endif # install_yes == install
